package com.eryos.gwt.estate.core.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eryos.gwt.estate.core.dao.IBeanADao;
import com.eryos.gwt.estate.core.domain.BeanA;
import com.eryos.gwt.estate.core.service.GreetingService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * GreetingService implementation.
 * 
 * @author combeol
 * @version $Revision$
 */
@Service("greetingService")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {

    /** Generated serialVersionUID. */
    private static final long serialVersionUID = 4405927377128006232L;

    /** The Logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(GreetingServiceImpl.class);

    @Autowired
    private IBeanADao beanDao;

    /**
     * {@inheritDoc}
     */
    @Override
    public String greetServer(final String input) throws IllegalArgumentException {
        LOGGER.debug("<greetServer>");

        BeanA servicea = new BeanA();

        // Escape data from the client to avoid cross-site script vulnerabilities.
        String escapedInput = escapeHtml(input);

        return "Hello, " + escapedInput + "!<br><br>You are on core/server side<br> Message from Bean : "
                + servicea.getTextForServer() + " <br/> DAO : " + beanDao.getDaoMessage();
    }

    /**
     * Escape an html string. Escaping data received from the client helps to prevent cross-site script
     * vulnerabilities.
     * 
     * @param html
     *            the html string to escape
     * @return the escaped string
     */
    private String escapeHtml(final String html) {
        if (html == null) {
            return null;
        }
        return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    }
}
